package gases.as3.librarys.core.utils.math.util
{
	
	import flash.utils.ByteArray;
	
	import gases.as3.librarys.core.utils.math.util.crypto.MD5;
	

	public class UserAuthCyptoUtil
	{
		
		/**
		 *用户信息类 
		 * 
		 */		
		public function UserAuthCyptoUtil()
		{
		}
		
		/**
		 * 对密码用MD5加密后，返回字符串
		 * @param $passwd
		 * @return 
		 * 
		 */		
		public static function passwordCypto($passwd:String):String
		{
			var md5degiset:String = MD5.hash($passwd);
			var ba:ByteArray = new ByteArray();
			for(var i:int =0; i<16;i++)
			{
				var hexstr:String = md5degiset.substr(i*2,2);
				var hex:int = parseInt(hexstr,16);
				ba.writeByte(hex)
			}
			return MD5.hashBinary(ba);
		}
		
		/**
		 * 返回一个Cypto加密的ByteArray
		 * @param $passwd
		 * @param $passkey
		 * @return 
		 * 
		 */		
		public static function passwdCypto($passwd:String,$passkey:ByteArray):ByteArray
		{
			var md5degiset:String = MD5.hash($passwd);
			var ba:ByteArray = new ByteArray();
			for(var i:int =0; i<16;i++)
			{
				var hexstr:String = md5degiset.substr(i*2,2);
				var hex:int = parseInt(hexstr,16);
				ba.writeByte(hex)
			}
			var genPass:ByteArray = new ByteArray();
			genPass.writeBytes($passkey);
			genPass.writeBytes(ba);
			var resultString:String = MD5.hashBinary(genPass);
			var resulteBa:ByteArray = new ByteArray();
			for(var j:int =0; j<16;j++)
			{
				var hexstr1:String = resultString.substr(j*2,2);
				var hex1:int = parseInt(hexstr1,16);
				resulteBa.writeByte(hex1)
			}
			return resulteBa;
		}
		
		/**
		 * 返回一个uint数字
		 * @param $passwd
		 * @return 
		 * 
		 */		
		public static function passwordCrc32($passwd:String):uint
		{
			var crc:CRC32 = new CRC32();
			var ba:ByteArray = new ByteArray();
			ba.writeUTFBytes($passwd);
			crc.update(ba,[0,ba.length]);
			return crc.getValue();
		}
	}
}